<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Jovem.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/JovemDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/controller/AbstractController.php'; ?>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/JuventudeSlz/utils/System.php'; ?>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/JuventudeSlz/utils/Niveis.php'; ?>

<?php

/**
 * Description of ControleServico
 *
 * @author JoasRamos
 */
class ControleServico extends AbstractController {

    private $dao, $bean;

    public function __construct($action) {
        $this->dao = new JovemDAO();
        $this->bean = new Jovem();
        parent::__construct($action);
    }

    public function create() {
        session_start();
        if (isset($_SESSION["servico"])) {
//        if (count($_POST > 0)) {
            //carrega objeto
            $bean = $this->carregaServico();

            //create
            if ($this->dao->insert($bean, array(
                        $bean->getNome(), $bean->getNomeCracha(), $bean->getRg(), $bean->getNascimento(),
                        $bean->getSexo(), $bean->getEndereco(), $bean->getCep(), $bean->getBairro(), $bean->getCidade(),
                        $bean->getHasAlimentacao(), $bean->getHasBarraca(), $bean->getHasTransporte(),
                        $bean->getDoEucaristia(), $bean->getHasEucaristia(), $bean->getDoSeminario(),
                        $bean->getDoAprofundamento(), $bean->getAlergiaMedicamento(), $bean->getAlergiaAlimento(),
                        $bean->getEmail(), $bean->getTelefone(), $bean->getNomeResp1(), $bean->getTelResp1(),
                        $bean->getNomeResp2(), $bean->getTelResp2(),
                        $bean->getIsParticipante(), $bean->getIsServico(), $bean->getServico()))) {
                System::showViewParameter("../view/create_sucess", array("id"), array(md5($bean->getRg())));
            } else {
                System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-persiste-item"));
            }
        } else {
            System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-permited"));
        }
    }

    private function carregaServico() {
        $s = $_SESSION['servico'];
        //
        $servico = $this->bean;
        //ajustes necessários
        $data = $s["inNascimento"];
        $data_format = date("Y-m-d", strtotime($data));
//        $ocupacao = $s["ocupacao"];
        $alimentacao = $s["alimentacao_propria"];
        $barraca = $s["barraca_propria"];
        $trans = $s["transporte_proprio"];
        $alergia_m = $s["alergia_medicamentos"];
        $alergia_a = $s["alergia_alimentos"];

        if ($alimentacao == '1')
            $servico->setHasAlimentacao(0);
        else
            $servico->setHasAlimentacao(1);

        if ($barraca == '1')
            $servico->setHasBarraca(1);
        else
            $servico->setHasBarraca(0);

        if ($trans == '1')
            $servico->setHasTransporte(0);
        else
            $servico->setHasTransporte(1);

        if ($alergia_a == '1')
            $servico->setAlergiaAlimento($s["info_alergia_ali"]);
        else
            $servico->setAlergiaAlimento("");

        if ($alergia_m == '1')
            $servico->setAlergiaMedicamento($s["info_alergia_med"]);
        else
            $servico->setAlergiaMedicamento("");

        //Carrega Jovem
        $servico->setNome($s["inNome"]);
        $servico->setNomeCracha($s["inCracha"]);
        $servico->setNascimento($data_format);
        $servico->setRg($s["inRg"]);
        $servico->setSexo($s["inSexo"]);
        $servico->setEndereco($s["inEnd"]);
        $servico->setCep($s["inCep"]);
        $servico->setBairro($s["inBairro"]);
        $servico->setCidade($s["inCidade"]);
        $servico->setEmail($s["inEmail"]);
        $servico->setTelefone($s["inTel"]);
        $servico->setNomeResp1($s["inNomeRes1"]);
        $servico->setTelResp1($s["inTelRes1"]);
        $servico->setNomeResp2($s["inNomeRes2"]);
        $servico->setTelResp2($s["inTelRes2"]);
        $servico->setIsParticipante(0);
        $servico->setIsServico(1);
        $servico->setServico($s["inServico"]);
        $servico->setHasEucaristia(1);
        $servico->setDoAprofundamento(0);
        $servico->setDoEucaristia(0);
        $servico->setDoSeminario(0);
        return $servico;
    }

    public function delete() {
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            if ($this->dao->delete($this->bean, $id)) {
                System::showSimpleView('../view/painel_admin');
            } else {
                System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-delete-item"));
            }
        } else {
            System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-permited"));
        }
    }

    public function personalizeAction($action) {
        
    }

    public function retrieve($id) {
        
    }

    public function buscarServico($id) {
        return $this->dao->findById($id);
    }

    public function listServico($rg) {
        return $this->dao->findAllServico($rg);
    }

}

if (isset($_REQUEST["code_action"])) {
    new ControleServico($_REQUEST["code_action"]);
}
?>
